//
//  ZCEssenceViewController.m
//  ZCBaiSi
//
//  Created by zc on 16/7/6.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "ZCEssenceViewController.h"
#import "ZCTitleButton.h"
#import "ZCAllTableViewController.h"
#import "ZCVideoTableViewController.h"
#import "ZCVoiceViewController.h"
#import "ZCImageViewController.h"
#import "ZCWordViewController.h"
@interface ZCEssenceViewController()<UIScrollViewDelegate>
//当前选中的标题按钮
@property (nonatomic, weak) ZCTitleButton *selectedTitleBtn;
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic, weak) UIScrollView *scrollView;
//标题栏
@property (nonatomic, weak) UIView *titleView;
@end

@implementation ZCEssenceViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    [self setupNav];
    
    [self setupChildViewControllers];
    
    [self setupScrollView];
    
    [self setupTitleView];
    
    //默认添加子控制器的view
    [self addChildVcView];
    
}

- (void)setupChildViewControllers{
    ZCAllTableViewController *all = [[ZCAllTableViewController alloc]init];
    [self addChildViewController:all];
    
    ZCVideoTableViewController *video = [[ZCVideoTableViewController alloc]init];
    [self addChildViewController:video];
    
    ZCVoiceViewController *voice = [[ZCVoiceViewController alloc]init];
    [self addChildViewController:voice];
    
    ZCImageViewController *image = [[ZCImageViewController alloc]init];
    [self addChildViewController:image];
    
    ZCWordViewController *word = [[ZCWordViewController alloc]init];
    [self addChildViewController:word];
}

- (void)setupNav{
     self.view.backgroundColor = ZCCommonBgColor;
    //顶部标题
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    //左边按钮
    
    //获取按钮图片的大小
    //button.zc_size = [button imageForState:UIControlStateNormal].size;
    //获取按钮的文字  button.currentTitle
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];
}

- (void)setupScrollView{
    // 不允许自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.delegate = self;
    scrollView.backgroundColor = ZCRandomColor;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.frame = self.view.bounds;
    [self .view addSubview:scrollView];
    self.scrollView = scrollView;
    
    //添加所有的子控制器的view到scrollView中
    NSInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(scrollView.zc_width * count, 0);
}

- (void)setupTitleView{
    //标题栏
    UIView *titleView = [[UIView alloc]init];
    //设置背景为半透明
//    titleView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
//    titleView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    titleView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.5];
    titleView.frame = CGRectMake(0, 64, self.view.zc_width, 35);
    [self.view addSubview:titleView];
    self.titleView = titleView;
    //添加标题
    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    NSUInteger count = titles.count;
    
    CGFloat titleBtnW = titleView.zc_width / count;
    CGFloat titleBtnH = titleView.zc_height;
    
    
    for (NSInteger i = 0; i < count; i++) {
        ZCTitleButton *titleBtn = [ZCTitleButton buttonWithType:UIButtonTypeCustom];
        titleBtn.tag = i;
        [titleBtn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:titleBtn];
        //设置数据
        [titleBtn setTitle:titles[i] forState:UIControlStateNormal];
        //设置frame
        titleBtn.frame = CGRectMake(i * titleBtnW, 0, titleBtnW, titleBtnH);
    }
    //按钮选中颜色
    ZCTitleButton *firstTitleBtn = titleView.subviews.firstObject;
    //底部的指示器
    UIView *indicatorView = [[UIView alloc]init];
    indicatorView.backgroundColor = [firstTitleBtn titleColorForState:UIControlStateSelected];
    indicatorView.zc_height = 1;
    indicatorView.zc_y = titleView.zc_height - indicatorView.zc_height;
    [titleView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    //立刻根据文字内容计算label的宽度
    [firstTitleBtn.titleLabel sizeToFit];
    indicatorView.zc_width = firstTitleBtn.titleLabel.zc_width + 10;
    indicatorView.zc_centerX = firstTitleBtn.zc_centerX;
    
    //默认情况：选中最前面的标题按钮
    firstTitleBtn.selected = YES;
    self.selectedTitleBtn = firstTitleBtn;
    
}

#pragma mark -- 监听点击
- (void)titleClick:(ZCTitleButton *)sender{
    //控制按钮状态
    self.selectedTitleBtn.selected = NO;
    sender.selected = YES;
    self.selectedTitleBtn = sender;
    //指示器
    [UIView animateWithDuration:0.25 animations:^{
      // CGFloat width = [sender.currentTitle sizeWithAttributes:@{NSFontAttributeName : sender.titleLabel.font}].width;
        //限制宽度或高度 计算换行的结果
//        [sender.currentTitle boundingRectWithSize:<#(CGSize)#> options:<#(NSStringDrawingOptions)#> attributes:<#(nullable NSDictionary<NSString *,id> *)#> context:<#(nullable NSStringDrawingContext *)#>]
        self.indicatorView.zc_width = sender.titleLabel.zc_width + 10;
        self.indicatorView.zc_centerX = sender.zc_centerX;
    }];
    
//    [self.selectedTitleBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    self.selectedTitleBtn = sender;
    //让scrollView 滚动到对应位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = (sender.tag) * self.scrollView.zc_width;
    [self.scrollView setContentOffset:offset animated:YES];
    
}

- (void)tagClick{
    ZCLogFunc
}

#pragma mark -- 添加子控制器的view

- (void)addChildVcView{
    //添加子控制器的view到scrollView中
    //子控制器的索引
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.zc_width;
    //取出子控制器
    UIViewController *childVc = self.childViewControllers[index];
    
//    if (childVc.view.superview) return;
//    if(childVc.view.window) return;
    if([childVc isViewLoaded]) return;
    
    
//    childVc.view.backgroundColor = ZCRandomColor;
    childVc.view.frame = self.scrollView.bounds;
   // [childVc.view setFrame:CGRectMake(self.scrollView.contentOffset.x, 0, self.scrollView.zc_width, self.scrollView.zc_height)];
    [self.scrollView addSubview:childVc.view];
}

#pragma mark -- <UIScrollViewDelegate>
//当使用setContentOffset/scrollRectVisible:animated:方法让scrollView开始了滚动动画，最后动画结束时，就会调用这个方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    [self addChildVcView];
}

//人为拖拽scrollView产生的滚动动画 触发这个方法的调用 结束减速的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //选中\点击对应的按钮
    NSUInteger index = scrollView.contentOffset.x / scrollView.zc_width;
    ZCTitleButton *tiltleBtn = self.titleView.subviews[index];
    //viewWithTag 默认所有空间的tag都是0 当index == 0时，viewWithTag：方法返回的就是self.titlesView
    [self titleClick:tiltleBtn];
    
    //添加子控制器的view
    [self addChildVcView];
    
}

@end
